//! @Author: DengLibin
//! @Date: Create in 2024-07-17 14:54:42
//! @Description:

use chrono::NaiveDateTime;
use chrono::{DateTime, Utc};
use derive_builder::Builder;
use proc_macro::table_name;
use proc_macro::GetFieldNames;
use proc_macro::HaveFieldType;
use proc_macro::StructToMap;
use serde::{Deserialize, Serialize};
use sqlx::FromRow;

use crate::sys::serialize_fn::{deserialize_date_time_utc, deserialize_naive_date_time, serialize_naive_date_time};
use crate::sys::serialize_fn::serialize_date_time_utc;

use my_trait::MustHaveTableName;
use my_trait::MustHaveFieldType;
use my_trait::Struct2Map;

#[derive(Debug, FromRow, Serialize, Deserialize)]
#[derive(Builder, GetFieldNames, StructToMap, HaveFieldType)]

#[table_name("user")]
pub struct OldUser {
    pub id: i64,
    
    pub name: Option<String>,
    #[serde(
        default,
        serialize_with = "serialize_date_time_utc", 
        deserialize_with = "deserialize_date_time_utc",
        skip_serializing_if = "Option::is_none"
    )]
    pub create_time: Option<DateTime<Utc>>, // 日期只能用Utc时间 0时区转成本地时间要+8小时
    #[serde(
        default,
        serialize_with = "serialize_date_time_utc",
        deserialize_with = "deserialize_date_time_utc",
        skip_serializing_if = "Option::is_none"
    )]

    pub update_time: Option<DateTime<Utc>>,
    pub create_by: Option<i64>,
    pub update_by: Option<i64>,
    pub user_login_name: Option<String>,
    pub deleted: Option<bool>,
    pub phone_num: Option<String>,
    pub passwords: Option<String>,
    pub status: Option<i32>,
    pub image_url: Option<String>,
    pub creator: Option<i64>,
    #[serde(
        default,
        serialize_with = "serialize_date_time_utc",
        deserialize_with = "deserialize_date_time_utc",
        skip_serializing_if = "Option::is_none"
    )]
    pub last_update_time: Option<DateTime<Utc>>,
    pub last_update_user: Option<i64>,
    pub user_group_id: Option<i64>,
    pub unit_id: Option<i64>,
    pub tenant_id: Option<i64>,
    pub flag: Option<i32>,
    pub user_logo_number: Option<String>,
    pub pass_update: Option<bool>,
    #[serde(
        default,
        serialize_with = "serialize_date_time_utc",
        deserialize_with = "deserialize_date_time_utc",
        skip_serializing_if = "Option::is_none"
    )]
    pub login_time: Option<DateTime<Utc>>,
    pub secret_grade_id: Option<i64>,
    pub allow_modify_psd: Option<i32>,
    pub ip_address: Option<String>,
    pub ip_int: Option<i64>,
    pub login_failed_count: Option<i32>,
    pub pid: Option<String>,
    pub public_key: Option<String>,
    pub mac: Option<String>,
    pub user_use_list: Option<String>,
    pub user_code: Option<String>,
}

#[derive(Debug, FromRow, Serialize, Deserialize)]
#[derive(Builder, GetFieldNames, StructToMap, HaveFieldType)]

#[table_name("user")]
pub struct User {
    pub id: i64,
    
    pub name: Option<String>,
    #[serde(
        default,
        serialize_with = "serialize_naive_date_time", 
        deserialize_with = "deserialize_naive_date_time",
        skip_serializing_if = "Option::is_none"
    )]
    pub create_time: Option<NaiveDateTime>,
    #[serde(
        default,
        serialize_with = "serialize_naive_date_time",
        deserialize_with = "deserialize_naive_date_time",
        skip_serializing_if = "Option::is_none"
    )]

    pub update_time: Option<NaiveDateTime>,
    pub create_by: Option<i64>,
    pub update_by: Option<i64>,
    pub user_login_name: Option<String>,
    pub deleted: Option<bool>,
    pub phone_num: Option<String>,
    pub passwords: Option<String>,
    pub status: Option<i32>,
    pub image_url: Option<String>,
    pub creator: Option<i64>,
    #[serde(
        default,
        serialize_with = "serialize_naive_date_time",
        deserialize_with = "deserialize_naive_date_time",
        skip_serializing_if = "Option::is_none"
    )]
    pub last_update_time: Option<NaiveDateTime>,
    pub last_update_user: Option<i64>,
    pub user_group_id: Option<i64>,
    pub unit_id: Option<i64>,
    pub tenant_id: Option<i64>,
    pub flag: Option<i32>,
    pub user_logo_number: Option<String>,
    pub pass_update: Option<bool>,
    #[serde(
        default,
        serialize_with = "serialize_naive_date_time",
        deserialize_with = "deserialize_naive_date_time",
        skip_serializing_if = "Option::is_none"
    )]
    pub login_time: Option<NaiveDateTime>,
    pub secret_grade_id: Option<i64>,
    pub allow_modify_psd: Option<i32>,
    pub ip_address: Option<String>,
    pub ip_int: Option<i64>,
    pub login_failed_count: Option<i32>,
    pub pid: Option<String>,
    pub public_key: Option<String>,
    pub mac: Option<String>,
    pub user_use_list: Option<String>,
    pub user_code: Option<String>,
}